package net.hvacapp.cmvrfforandroid.ui.product;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.product.model.ProductDataResp;
import net.hvacapp.cmvrfforandroid.ui.product.model.ProductProperty;
import net.hvacapp.cmvrfforandroid.ui.product.model.PropertyData;
import net.hvacapp.cmvrfforandroid.ui.product.view.PropertyItemView;
import net.hvacapp.cmvrfforandroid.widget.CellSeperateLineView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hailonghan on 15/4/6.
 */
@EActivity(R.layout.product_detail)
public class ProductDetailActivity extends BaseActivity {


    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;

    private QuickAdapter<ProductProperty> mQuickAdapter;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {

    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "产品信息", null);

        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<ProductProperty>(context, R.layout.product_detail_cell_item) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProductProperty item) {
                    LinearLayout swjInfoLl = helper.getView(R.id.ll_product_group);
                    swjInfoLl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //控制显示隐藏
                            item.setClosed(!item.isClosed());
                            if (item.isClosed()) {
                                helper.setImageResource(R.id.iv_selected_mark, R.drawable.arrow_down);
                                helper.setVisible(R.id.ll_property_content, false);
                            }
                            else {
                                helper.setImageResource(R.id.iv_selected_mark, R.drawable.arrow_up);
                                helper.setVisible(R.id.ll_property_content, true);
                            }
                        }
                    });

                    helper.setText(R.id.tv_title, item.getGroup());

                    //添加属性
                    LinearLayout propertyContent = helper.getView(R.id.ll_property_content);
                    propertyContent.removeAllViews();
                    if (item.getProperties().size() > 0) {
                        propertyContent.setVisibility(View.VISIBLE);
                    }
                    else {
                        propertyContent.setVisibility(View.GONE);
                    }

                    for (final PropertyData property : item.getProperties()) {
                        CellSeperateLineView line = new CellSeperateLineView(ProductDetailActivity.this);
                        propertyContent.addView(line);

                        final PropertyItemView itemView = new PropertyItemView(ProductDetailActivity.this);
                        itemView.setTitleString(property.getName());
                        itemView.setValueString(property.getValue());
                        propertyContent.addView(itemView);

                    }

                }
            };
        }




    }

    private void initEvent() {

    }

    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        String tag = intent.getStringExtra("tag");
        String guidId = intent.getStringExtra("guidId");
        loadProductProperty(tag, guidId);

        View header = View.inflate(this, R.layout.product_detail_header_view, null);
        ImageView imageIv = (ImageView) header.findViewById(R.id.iv_product);

        String picUrl = HttpConstant.HTTP_GET_ONE_PRODUCT_PICTURE +
                "?tk=" + AccountManager.getInstance().getToken() +
                "&tag=" + tag +
                "&id=" + guidId;

        ImageLoader.getInstance().displayImage(picUrl, imageIv);

        mTempleteLv.addHeaderView(header);
        mTempleteLv.setAdapter(mQuickAdapter);
    }

    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void loadProductProperty(String tag, String guidId) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", guidId);
        param.put("tag", tag);
        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ONE_PRODUCT_DETAIL, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<ProductDataResp>() {
            @Override
            public void onSuccess(ProductDataResp obj) {
                // 成功
                mQuickAdapter.clear();
                mQuickAdapter.addAll(obj.getGetProductResult().getPropertyGroups());
            }

            @Override
            public void onFailure(String response) {

            }
        });
    }

}
